package com.csc.banking.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.csc.banking.form.TransferFundsForm;
import com.csc.banking.model.Balance;
import com.csc.banking.model.Transaction;
import com.csc.banking.service.Services;

@Controller
public class transferFundController 
{
	@Autowired
	private Services services;
	
	@RequestMapping(value = "/transferFunds", method = RequestMethod.GET)
	public String tryTransfer(Model model, HttpServletRequest request,
			HttpServletResponse response) 
	{
		Transaction transaction=(Transaction)request.getSession().getAttribute("transaction");		
		if(transaction!=null) 
		{
			model.addAttribute("transaction", transaction);
			model.addAttribute("depositAccount",services.getDepositAccount(transaction.getCustomer()));	
			model.addAttribute("tfForm", new TransferFundsForm());
		}
		return "transferFunds";
	}
	
	@RequestMapping(value = "/customer", method = RequestMethod.GET)
	public String customer(Model model, HttpServletRequest request,
			HttpServletResponse response) 
	{
		Transaction transaction=(Transaction)request.getSession().getAttribute("transaction");		
		if(transaction!=null) model.addAttribute("transaction", transaction);
		List<Transaction> transactions=services.getAllTransactionByCustomer(transaction.getCustomer());
		
		model.addAttribute("transactions", transactions);
		return "customer";
	}
	
	
	@RequestMapping(value="/tryValidate", method=RequestMethod.POST)
    public String tryValidate(@Valid TransferFundsForm tfForm, BindingResult result, Model m, HttpServletRequest request,
			HttpServletResponse response) 
	{
		Transaction transaction=(Transaction)request.getSession().getAttribute("transaction");		
		if(transaction!=null) m.addAttribute("transaction", transaction);
		m.addAttribute("depositAccount",services.getDepositAccount(transaction.getCustomer()));	
		if(result.hasErrors()) 
        {
			m.addAttribute("tfForm", new TransferFundsForm());			
			System.out.println("error\n\n\n");			
			return "transferFunds";            
        }
         
        System.out.println("success\n\n\n");
        m.addAttribute("tfForm", new TransferFundsForm());
        return "transferFunds";
    }
	
	@RequestMapping(value = "/doTransfer", method = RequestMethod.GET)
	public String doTransfer(Model model, HttpServletRequest request,
			HttpServletResponse response) 
	{
		
		String fromBalance= request.getParameter("selectedAccountNumber");
		String toBalance= request.getParameter("selectedDes");
		String amount= request.getParameter("amount");
		
		Transaction transaction=(Transaction)request.getSession().getAttribute("transaction");		
		if(transaction!=null) model.addAttribute("transaction", transaction);
		int firstValidationresult=services.firstValidate(transaction,fromBalance,toBalance,amount);
		if(firstValidationresult==0)
		{
			Balance fromAccount=services.getBalance(fromBalance);
			Balance toAccount=services.getBalance(toBalance);
			int transferResult=services.transferFund(transaction, fromAccount, toAccount, Double.valueOf(amount));
		}
		else
		{
			System.out.println(firstValidationresult);
		}
		
		List<Balance> balances=services.getbalances(transaction.getCustomer());
		model.addAttribute("tfForm", new TransferFundsForm());
		model.addAttribute("balances", balances);
		return "transferFunds";
	}
}
